﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class ManageInstructionModel
    {
        ManageInstructionScreenData ManageInstructionScreenData { set; get; }
        public static ManageInstructionModel Build(ManageInstructionScreenData scrData)
        {
            //_ScrData = scrData;
            ManageInstructionModel model = new ManageInstructionModel();
            model.InitData();
            model.ManageInstructionScreenData = scrData;
            foreach (var item in scrData.Company)
            {
                model.CompanyList.Add(new SelectListItem() { Text = item.CompanyNameTh, Value = item.CompanyId.ToString() });
            }
            foreach (var item in scrData.FinancialInstrument)
            {
                model.SelectFinancialInstrumentList.Add(new SelectListItem() { Text = item.ValueEN, Value = item.ID.ToString() });
            }
            foreach (var item in scrData.AssetClass)
            {
                model.SelectAssetClassList.Add(new SelectListItem() { Text = item.AssetClassTH, Value = item.AssetClassID.ToString() });
            }
            foreach (var item in scrData.TransactionType)
            {
                model.SelectTransactionList.Add(new SelectListItem() { Text = item.ValueTH, Value = item.ValueEN.ToString() });
            }
            foreach (var item in scrData.InstructionStatus)
            {
                model.SelectInstructionStatusList.Add(new SelectListItem() { Text = item.ValueTH, Value = item.Key.ToString() });
            }

            return model;
        }


        [Required(ErrorMessage = "โปรดเลือก")]
        public int? SelectCompanyID { set; get; }
        public int? SelectFinancialInstrument { set; get; }
        public int? SelectAssetClass { set; get; }
        public DateTime? CreateDateForm { set; get; }
        public DateTime? CreateDateTO { set; get; }
        public String RefNo { set; get; }
        public String SelectTransaction { set; get; }
        public String SelectInstructionStatus { set; get; }


        public String BtnSearch { set; get; }
        public String BtnAddnew { set; get; }


        public IList<SelectListItem> CompanyList { get; set; }
        public IList<SelectListItem> SelectFinancialInstrumentList { get; set; }
        public IList<SelectListItem> SelectAssetClassList { get; set; }
        public IList<SelectListItem> SelectTransactionList { get; set; }
        public IList<SelectListItem> SelectInstructionStatusList { get; set; }

        public List<ManageInstructionDTO> ManageInstructionDTOList { get; set; }

        public ManageInstructionModel()
        {
            CompanyList = new BindingList<SelectListItem>();
            SelectFinancialInstrumentList = new BindingList<SelectListItem>();
            SelectAssetClassList = new BindingList<SelectListItem>();
            SelectTransactionList = new BindingList<SelectListItem>();
            SelectInstructionStatusList = new BindingList<SelectListItem>();

            ManageInstructionDTOList = new List<ManageInstructionDTO>();
        }

        public void InitData()
        {
            CompanyList.Clear();
            CompanyList.Add(WebUtil.SelectListSelectDefault());

            SelectFinancialInstrumentList.Clear();
        //    SelectFinancialInstrumentList = WebData.FinancialInstrument;
            SelectFinancialInstrumentList.Insert(0, WebUtil.SelectListAllDefault());


            SelectAssetClassList.Clear();
          //  SelectAssetClassList = WebData.AssetClass;
            SelectAssetClassList.Insert(0, WebUtil.SelectListAllDefault());

            SelectTransactionList.Clear();
         //   SelectTransactionList = WebData.TransactionType;
            SelectTransactionList.Insert(0, WebUtil.SelectListAllDefault());


            SelectInstructionStatusList.Clear();
          //  SelectInstructionStatusList = WebData.InstructionStatus;
            SelectInstructionStatusList.Insert(0, WebUtil.SelectListAllDefault());

            ManageInstructionDTOList.Clear();



            SelectCompanyID = null;
            SelectFinancialInstrument = null;
            SelectAssetClass = null;
            CreateDateForm = null;
            CreateDateTO = null;
            RefNo = null;
            SelectTransaction = null;
            SelectInstructionStatus = null;
        }

        public SearchInstructionFilter ToSearchInstructionFilter()
        {
            SearchInstructionFilter filter = new SearchInstructionFilter();
          //  filter.AssetClass = SelectAssetClass;
            filter.CompanyID = SelectCompanyID;
            filter.CreateDateForm = CreateDateForm;
            filter.CreateDateTO = CreateDateTO;
            filter.RefNo = RefNo;
            filter.SelectTransaction = SelectTransaction;
            filter.SelectInstructionStatus = SelectInstructionStatus;
            if (SelectAssetClass.GetValueOrDefault()>0)
                filter.AssetClass = SelectAssetClass.GetValueOrDefault();

            if (SelectFinancialInstrument.GetValueOrDefault() > 0)
                filter.FinancialInstrument = SelectFinancialInstrument.GetValueOrDefault();
            return filter;
        }
    }
}